package com.wcar.sso.realm;

import com.wcar.common.entity.User;
import com.wcar.sso.feign.UserFeign;
import com.wcar.common.vo.Result;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;

public class MyRealm extends AuthorizingRealm {
    @Resource
    private UserFeign userFeign;
    //sso只负责登录认证 不负责数据查询
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1.获取账号
        String username = (String) token.getPrincipal();
        //2.根据账号查询用户信息
        System.out.println(userFeign);
        Result<User> result = userFeign.selectByName(username);
        if(result.getCode()!=500){
            User user = (User) result.getData();
            ByteSource salt=ByteSource.Util.bytes(user.getSalt());
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(user,user.getPassword(),salt,this.getName());
            return info;
        }
        return null;
    }
}